package com.example.john.foodstore;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MyDishes extends AppCompatActivity {


    private ListView lv;
    private LayoutInflater inflater;
    private List<Dishes> mydishes=new ArrayList<Dishes>();
    //private String[] data=new String[]{"abc","bdc","xxer"};
    //private int[] snaps=new int[]{R.drawable.a1,R.drawable.a2,R.drawable.a3};
    private DishesAdapter adapter;

    /*private void init(){
        for (int i=0;i<snaps.length;i++){
            Order o=new Order();
            o.setOrderName("order..."+i);
            o.setDetail("detail..." + i);
            o.setSnap(snaps[i]);
            orders.add(o);
        }
    }*/


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_dishes);
        //init();
        inflater=getLayoutInflater();
        lv=(ListView)findViewById(R.id.lv2);
        Dishes dishes=new Dishes();
        dishes.setDishesName("111");
        dishes.setDishesPrice("222");
        dishes.setDishesClass("333");
        mydishes.add(dishes);
        adapter=new DishesAdapter(mydishes,inflater);
        lv.setAdapter(adapter);
//        mydishes.add(dishes);
//        adapter.setMydishes(mydishes);
       /* lv=(ListView)findViewById(R.id.lv2);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MyDishes.this, data[position].toString(), Toast.LENGTH_LONG).show();
                Log.i("Amy", data[position]);
            }
        });*/

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(resultCode==0){

            /*((EditText)findViewById(R.id.ed1)).setText(data.getStringExtra("dishesName"));
            ((EditText)findViewById(R.id.ed2)).setText(data.getStringExtra("dishesClass"));
            ((EditText)findViewById(R.id.ed3)).setText(data.getStringExtra("dishesPrice"));*/
            String dName=data.getStringExtra("dishesName");
            String dClass=data.getStringExtra("dishesClass");
            String dPrice=data.getStringExtra("dishesPrice");
            Bitmap dPicture=data.getParcelableExtra("dishesPicture");
            //Log.i("Amy", dName+".."+dClass+".."+dPrice);
            Dishes dishes_add=new Dishes();
            dishes_add.setDishesName(dName);
            dishes_add.setDishesClass(dClass);
            dishes_add.setDishesPrice(dPrice);
            dishes_add.setDishesPicture(dPicture);
            mydishes.add(dishes_add);
            adapter.setMydishes(mydishes);
            Log.i("Amy", "..."+mydishes.size());
            adapter.notifyDataSetChanged();
        }
    }

    public void Add(View v){
        Intent intent=new Intent();
        intent.setClass(this,AddDishes.class);
        //lv.setAdapter(adapter);
        startActivityForResult(intent, 0);


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_my_dishes, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
